# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  #  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password

  require 'gchart'
  
  def index
    new_incident, param_inci = Incident.agent(params[:page],nil,nil,nil,nil,nil)
    Incident.create(new_incident)
    
    @machine = Incident.getmachine
    @salle = Salle.all

    libelle, value, libelle2, value2, libelle3, value3, libelle4, value4 = Incident.getstat


    range = []
    for nb in 0..value.max
      range << nb.to_s
    end

    range2 = []
    for nb2 in 0..value2.max
      range2 << nb2.to_s
    end

    

    if !libelle.empty? && !value3.empty?
    @graph = Gchart.pie_3d(:title => 'Incidents par Année',
                            :size => '500x300',
                            :data => value,
                            :labels => libelle)
else
      @graph = nil
    end


      if !libelle2.empty? && !value2.empty?
    @graph2 = Gchart.pie_3d(:title => "Incidents par Mois",
                           :size => "500x300",
                           :data => value2,
                           :labels => libelle2)
else
      @graph2 = nil
    end

if !libelle3.empty? && !value3.empty?
    @graph3 = Gchart.bar(:title => 'Incidents mois/regle',
                            :title_size => 12,
                            :size => '500x300',
                            :data => value3,
                            :bar_colors => '00FF00',
                            :axis_with_labels => ['x','y'],
                           :bar_width_and_spacing => '35,45',
                           :axis_labels => [libelle3.insert(10,""), range])
else
      @graph3 = nil
    end

if !libelle4.empty? && !value4.empty?
    @graph4 = Gchart.bar(:title => "Incidents mois/salle",
                           :title_size => 12,
                           :size => "500x300",
                           :data => value4,
                           :bar_colors => '8A2BE2',
                           :axis_with_labels => ['x','y'],
                           :bar_width_and_spacing => '35,90',
                           :axis_labels => [libelle4.insert(20,""), range2])
else
      @graph4 = nil
    end
    #    @graph = Gchart.pie_3d(:title => "test", :size => "450x200",  :data => [50, 20, 30], :labels => ["hhh", "uuu", "ddd"])
    respond_to do |format|
      format.html
    end
  end

  
  protected
  include ApplicationHelper
  
  def check_authentification
    return true if authenticated?
    route_to_login "Vous devez être connecté pour effectuer cette action."
  end

  def route_to_login(message = nil)
    flash[:error] = message if message
    session[:original_uri] = request.request_uri if request.get?
    redirect_to login_url
    false
  end
  
  # Fonctionnalité "Se souvenir de moi"
# private
  # def current_user
    # @current_user ||= User.find_by_auth_token( cookies[:auth_token]) if cookies[:auth_token]
  # end
  # helper_method :current_user
  
end
